//
//  ZJBankAccountVC.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/7/10.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJBankAccountVC.h"
#import "ZJBankAccountView.h"
#import "ZJAddBankVC.h"
#import "ZJBankCell.h"

@interface ZJBankAccountVC ()<UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (strong, nonatomic) ZJBankAccountView * bcView;
@property (strong, nonatomic)ZJUserModel * model;
@property (strong, nonatomic)UIAlertController *alertController;
@end

@implementation ZJBankAccountVC

-(ZJBankAccountView *)bcView{
    if (!_bcView) {
        self.bcView = [[[NSBundle mainBundle] loadNibNamed:@"ZJBankAccountView" owner:self options:nil] lastObject];
    }
    return _bcView;
}

-(ZJUserModel *)model{
    if (!_model) {
        _model = GET_USERINFO;
    }
    return _model;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    if ([self.model.bank isEqualToString:@""] && [self.model.account isEqualToString:@""]) {
        
        self.tableview.hidden = YES;
        [self.bcView.addBankBtn addTarget:self action:@selector(addBank) forControlEvents:UIControlEventTouchUpInside];
        [self.bcView.addBankBtn setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
        self.bcView.frame = CGRectMake(0, 64, SCREEN_WIDTH, 350);
        [self.view addSubview:self.bcView];
        
    }else{
        self.tableview.hidden = NO;
        [self.bcView removeFromSuperview];
        [self.tableview reloadData];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableview registerNib:[UINib nibWithNibName:NSStringFromClass([ZJBankCell class]) bundle:nil] forCellReuseIdentifier:@"ZJBankCell"];
    
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

-(void)addBank{

    ZJAddBankVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJAddBankVC"];
    vc.model = self.model;
    
    self.alertController = [UIAlertController alertControllerWithTitle:@"选择账户类型"
                                                               message:nil
                                                        preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [_alertController addAction:cancelAction];
    
    UIAlertAction *moreAction = [UIAlertAction actionWithTitle:@"添加支付宝" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        vc.type = @"支付宝";
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [_alertController addAction:moreAction];
    
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"添加银行卡" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        vc.type = @"银行卡";
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [_alertController addAction:OKAction];
    
    [self presentViewController:_alertController animated:YES completion:nil];
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([self.model.bank isEqualToString:@""] && [self.model.account isEqualToString:@""]) {
        ZJBankCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZJBankCell"];
        cell.bankName.text = @"";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.bankCard.text = @"";
        
        return cell;
    }else{
        ZJBankCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZJBankCell"];
        cell.bankName.text = self.model.bank;
        NSString *num = [self.model.account substringFromIndex:(self.model.account.length - 4)];
        NSString * lefNem = [self.model.account substringToIndex:4];
        cell.selectionStyle = UITableViewCellSelectionStyleNone; 
        if ([self.model.bank isEqualToString:@"支付宝"]) {
            lefNem = [self.model.account substringToIndex:3];
            cell.bankCard.text = [NSString stringWithFormat:@"%@ **** %@",lefNem,num];
        }else{
            cell.bankCard.text = [NSString stringWithFormat:@"%@ **** **** %@",lefNem,num];
        }
        
        if ([cell.bankName.text isEqualToString:@"支付宝"]) {
            cell.bgImg.image = [UIImage imageNamed:@"yhgl_bg_zfb"];
            cell.bankTypeTitle.hidden = YES;
        }
        if ([cell.bankName.text isEqualToString:@"中国银行"]) {
            cell.bgImg.image = [UIImage imageNamed:@"yhgl_bg_zgyh"];
        }
        if ([cell.bankName.text isEqualToString:@"中国农业银行"]) {
            cell.bgImg.image = [UIImage imageNamed:@"yhgl_bg_nyyh"];
        }
        if ([cell.bankName.text isEqualToString:@"中国工商银行"]) {
            cell.bgImg.image = [UIImage imageNamed:@"yhgl_bg_gsyh"];
        }
        if ([cell.bankName.text isEqualToString:@"中国建设银行"]) {
            cell.bgImg.image = [UIImage imageNamed:@"yhgl_bg_jsyh"];
        }
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 300;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZJAddBankVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJAddBankVC"];
    vc.model = self.model;
    vc.type = @"删除";
    [self.navigationController pushViewController:vc animated:YES];
}
@end
